package sisi.payment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sisi.General;
import sisi.ListaStandardWindow;


/**
 * PaymentsmodeWindow.java - Controller class for <i>Payments mode window</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 16/11/2013
 * 
 */

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class PaymentsmodeWindow extends Window implements
		org.zkoss.zk.ui.ext.AfterCompose, ListaStandardWindow {
	
	// listbox boxPaymentsmode
	private Window windowPaymentsmode, windowEditPaymentmode;
	private Textbox tbCerca;
	private Listbox boxPaymentsmode;

	private List tutti_i_paymentsmode;
	private boolean lNuovo;
	private Toolbarbutton buttonSeleziona;

	private ListModelList listModelList;
	private Integer idSelezionato = 0;

	protected Map<String, Object> args;
	private Object paymentmodeItem;
	private Paymentmode modPaymentmode;
	// edit
	private Intbox id;
	private Textbox code, mode;

	public void onCreate$windowPaymentsmode(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N"
				: tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		tutti_i_paymentsmode = new PaymentsmodeController().getPaymentmodeList(null);
		listModelList.addAll(tutti_i_paymentsmode);
		boxPaymentsmode.setModel(listModelList);
		boxPaymentsmode.setItemRenderer(new PaymentsmodeItemRenderer());
		
		if (idSelezionato!=null && idSelezionato!=0){
			for (int i = 0; i < tutti_i_paymentsmode.size(); i++) {
				Paymentmode paymentmode = (Paymentmode)tutti_i_paymentsmode.get(i);
				if (paymentmode.getPayid()==idSelezionato.intValue()){
					boxPaymentsmode.setSelectedIndex(i);
					break;
				}
			}
		}

	}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}
	
	public void onClick$buttonCerca(Event event) {
		cerca();
	}
	
	
	private void cerca(){
		listModelList = new ListModelList();
		tutti_i_paymentsmode = new PaymentsmodeController().getPaymentmodeList(tbCerca
				.getValue());
		listModelList.addAll(tutti_i_paymentsmode);
		boxPaymentsmode.setModel(listModelList);
		boxPaymentsmode.setItemRenderer(new PaymentsmodeItemRenderer());		
	}
	
	
	
	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}
	
	
	public void onClick$buttonSeleziona(Event event) {

		seleziona();

	}

	public void seleziona() {
		if (boxPaymentsmode.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oPaymentmode = boxPaymentsmode.getSelectedItem().getAttribute(
					"rigaPaymentmodeSelezionato");
			String codiceSelezionato = ""+((Paymentmode) oPaymentmode).getPayid();
			Sessions.getCurrent().setAttribute("codicePaymentmodeSelezionato",
					codiceSelezionato);
			windowPaymentsmode.detach();
		}
	}


	@Override
	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	@Override
	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}
	
	private void modifica(boolean nuovo){
		try {

			int nIndex = boxPaymentsmode.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a Payment Mode to modify", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxPaymentsmode", boxPaymentsmode);
				Window finestraEditPaymentmode = (Window) Executions.createComponents(
						"/editPaymentmode.zul", null, map);
				finestraEditPaymentmode.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}
	
	
	
	@Override
	public void onClick$buttonCancella(Event event) {
		// TODO Auto-generated method stub
		
	}	

	public class PaymentsmodeItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Paymentmode) data).getPayid()).setParent(li);
			new Listcell(((Paymentmode) data).getPaycode()).setParent(li);
			new Listcell(((Paymentmode) data).getPaymode()).setParent(li);
			li.setAttribute("rigaPaymentmode", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}

	
	
	// EDIT PAYMENT MODE: NEW/MODIFY
	public void onCreate$windowEditPaymentmode(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		
		boxPaymentsmode = (Listbox) args.get("boxPaymentsmode");

		if (args.containsKey("lNuovo")) {
			lNuovo = true;
		} else {
			lNuovo = false;
		}

		if (!lNuovo) {
			paymentmodeItem = boxPaymentsmode.getSelectedItem().getAttribute("rigaPaymentmode");
			modPaymentmode = new PaymentsmodeController()
					.refreshPaymentmode((Paymentmode) paymentmodeItem);
			paymentmodeItem = modPaymentmode;
			windowEditPaymentmode.setTitle("Edit Payment Mode");
			code.setFocus(true);
			id.setValue(modPaymentmode.getPayid());
			code.setValue(modPaymentmode.getPaycode());
			mode.setValue(modPaymentmode.getPaymode());
		} else {
			windowEditPaymentmode.setTitle("New Payment Mode");
			code.setFocus(true);
		}

	}
	
	public void onClick$btnAnnulla(Event event){
		windowEditPaymentmode.detach();
	}
	
	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modPaymentmode = new Paymentmode();
		} else {

		}
		String testoMsg = "";
		
		modPaymentmode.setPayid(id.getValue());
		modPaymentmode.setPaycode(General.toUpperAncheNull(code.getValue()));
		modPaymentmode.setPaymode(General.toUpperAncheNull(mode.getValue()));
		
		if (lNuovo){
			new PaymentsmodeController().addPaymentmode(modPaymentmode);
			testoMsg = "Adding Payment Mode: "+ General.trimNull( modPaymentmode.getPaymode() );
		} else {
			new PaymentsmodeController().updatePaymentmode(modPaymentmode);
			testoMsg = "Updating Payment Mode: "+ General.trimNull( modPaymentmode.getPaymode() );			
		}
		
		General.showNotify( testoMsg, windowEditPaymentmode);		
		
		windowEditPaymentmode.detach();
		
		// aggiorna listbox
		
		if (lNuovo){
			ListModelList lml = (ListModelList) boxPaymentsmode.getListModel();
			lml.add(modPaymentmode);
			boxPaymentsmode.setSelectedIndex(lml.indexOf(modPaymentmode));
		} else {
			ListModelList lml = (ListModelList) boxPaymentsmode.getListModel();
			Object oPaymentmodeItem = boxPaymentsmode.getSelectedItem().getAttribute("rigaPaymentmode");
			if (lml.indexOf(oPaymentmodeItem) == -1) {
				// non trovato... quindi non lo aggiorno
			} else {
				lml.set(lml.indexOf(oPaymentmodeItem), modPaymentmode);
			}
		}		
	}
	
	
}
